﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace IngredSubTool
{
    /// <summary>
    /// Handles retrieving the ingredient/substitution information from the flat storage file and converting it to the relevant data classes to be used later.
    /// </summary>
    public class DataLoader
    {
        public static List<SearchCategory> SearchCategories { get; set; }
        public static List<Substitution> Substitutions { get; set; }

        public static void LoadCategories()
        {
            SearchCategories = new List<SearchCategory>();

            StreamReader reader = new StreamReader("Resources\\categories.txt");
            try
            {
                while (reader.Peek() != -1)
                {
                    String catLine = reader.ReadLine();
                    if (!catLine.StartsWith("#"))
                    {
                        SearchCategories.Add(new SearchCategory(catLine));
                    }
                }
            }
            catch (Exception e)
            {
                //TODO Text IO Bad. How to handle this?
                MessageBox.Show(e.Message);
            }
            finally
            {
                reader.Close();
            }
        }

        public static void LoadSubstitutions()
        {
            Substitutions = new List<Substitution>();

            StreamReader reader = new StreamReader("Resources\\substitutions.txt");
            try
            {
                while (reader.Peek() != -1)
                {
                    String subLine = reader.ReadLine();
                    if (!subLine.StartsWith("#"))
                    {
                        String[] vals = subLine.Split(';');
                        Substitutions.Add(new Substitution(vals[0], vals[1], vals[2], vals[3]));
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            finally
            {
                reader.Close();
            }
        }



    }
}
